package com.s4j.core.manager.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.manager.model.SysButton;
import com.s4j.core.manager.model.SysMenu;
import com.s4j.core.manager.model.SysModule;
import com.s4j.core.manager.model.SysProject;
import com.s4j.core.manager.model.SysUser;
import com.s4j.core.manager.module.listener.ModuleInitListener;
import com.s4j.core.manager.service.ManagerService;

@Controller("com.s4j.core.manager.action.ManagerAction")
@Scope("prototype")
public class ManagerAction extends ActionSupport {
	
	private static final long serialVersionUID = 7531473233465529906L;
	
	private static Logger log = Logger.getLogger(ManagerAction.class);
	
	private String projectNo;
	
	private String homePage;
	
	@Resource
	private ManagerService managerService;

	public String toLogin() {
		log.info("login");
		return Action.SUCCESS;
	}

	public String login() {
		log.info("login");
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String username = request.getParameter("username");
		
		String password = request.getParameter("password");
		
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			request.setAttribute("lgnTip", "用户名或密码错误！");
			return "loginErr";
		}
		
		SysUser user = managerService.login(username, password);
		
		if(user != null) {
			request.getSession().setAttribute(Constant.BG_LOGIN_USER, user);
			return Action.SUCCESS;
		}
		
		request.setAttribute("lgnTip", "用户名或密码错误！");
		return "loginErr";
	}
	
	public String index() {
		ActionContext context = ActionContext.getContext();
		
		List<SysProject> projectTree = managerService.loadMainLeftTree();
		
		context.put("projectTree", projectTree);
		
		return Action.SUCCESS;
	}
	
	public String showModulePage() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String moduleNo = request.getParameter("_mduno");
		
		SysModule module = managerService.findModuleByNo(moduleNo);
		
		if(!StringUtils.isNullOrBlank(module.getModuleInitListener())) {
			WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
			ModuleInitListener initListener = webApplicationContext.getBean(module.getModuleInitListener(), ModuleInitListener.class);
			initListener.init(module, request);
		}
		
		SysModule pModule = module.getParent();
		
		if(pModule == null) {
			
			this.setProjectNo(module.getProject().getProjectNo());
			
		} else {
		
			while(pModule.getIsLeaf().shortValue() == Constant.SYS_MODULE_IS_LEAF_YES) {
				
				pModule = pModule.getParent();
				
			}
			
			this.setProjectNo(pModule.getProject().getProjectNo());
		}
		
		
		this.setHomePage(module.getHomePage());
		
//		setModuleButton(module, request);
		
		return Action.SUCCESS;
	}
	
	public String logout() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.getSession().removeAttribute(Constant.BG_LOGIN_USER);
		return Action.SUCCESS;
	}

	private void setModuleButton(SysModule module, HttpServletRequest request) {
		
		List<SysMenu> menus = module.getMenus();
		
		for(int i=0; i<menus.size(); i++) {
			
			SysMenu menu = menus.get(i);
			
			if(menu.getValid().shortValue() == Constant.SYS_MENU_VALID_NO) {
				
				menus.remove(i);
				
				i--;
			} else {
				
				List<SysButton> buttons = managerService.findButtonByMenu(menu.getMenuId());
				
				//去掉失效的按钮
				for(int j=0; j<buttons.size(); j++) {
					
					if(buttons.get(j).getValid().shortValue() == Constant.SYS_BUTTON_VALID_NO) {
						
						buttons.remove(j);
						
						j--;
					}
				}
				
				request.setAttribute(menu.getMenuNo(), buttons);
			}
		}
	}

	public String getProjectNo() {
		return projectNo;
	}

	public void setProjectNo(String projectNo) {
		this.projectNo = projectNo;
	}

	public String getHomePage() {
		return homePage;
	}

	public void setHomePage(String homePage) {
		this.homePage = homePage;
	}
}
